package org.hot100_0.leetcode560;

public class Solution {
    public static void main(String[] args) {
        int[] nums = {1, 1, 1};
        int count = subarraySum(nums, 2);
        System.out.println(count);

    }

    public static int subarraySum(int[] nums, int k) {

        int n = nums.length;
        int count = 0;

        int[] preSum = new int[n + 1];
        preSum[0] = 0;
        for (int i = 1; i <= n; i++) {
            preSum[i] = preSum[i - 1] + nums[i - 1];
        }

        for (int i = 0; i <= n; i++) {
            for (int j = i + 1; j <= n; j++) {
                if (preSum[j] - preSum[i] == k) {
                    count++;
                }
            }
        }

        return count;
    }
}
